package org.dromara.hm.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.validate.ProcessingAgentGroup;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.hm.domain.vo.HmTodoItemsVo;
import org.dromara.hm.domain.bo.HmTodoItemsBo;
import org.dromara.hm.service.IHmTodoItemsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 代办事项
 *
 * @author zy Hu
 * @date 2024-12-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hm/todoItems")
public class HmTodoItemsController extends BaseController {

    private final IHmTodoItemsService hmTodoItemsService;

    /**
     * 查询代办事项列表
     */
    @SaCheckPermission("hm:todoItems:list")
    @GetMapping("/list")
    public TableDataInfo<HmTodoItemsVo> list(HmTodoItemsBo bo, PageQuery pageQuery) {
        return hmTodoItemsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出代办事项列表
     */
    @SaCheckPermission("hm:todoItems:export")
    @Log(title = "代办事项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HmTodoItemsBo bo, HttpServletResponse response) {
        List<HmTodoItemsVo> list = hmTodoItemsService.queryList(bo);
        ExcelUtil.exportExcel(list, "代办事项", HmTodoItemsVo.class, response);
    }

    /**
     * 获取代办事项详细信息
     *
     * @param todoId 主键
     */
    @SaCheckPermission("hm:todoItems:query")
    @GetMapping("/{todoId}")
    public R<HmTodoItemsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long todoId) {
        return R.ok(hmTodoItemsService.queryById(todoId));
    }

    /**
     * 新增代办事项
     */
    @SaCheckPermission("hm:todoItems:add")
    @Log(title = "代办事项", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HmTodoItemsBo bo) {
        return toAjax(hmTodoItemsService.insertByBo(bo));
    }

    /**
     * 处理代办
     */
    @Log(title = "代办事项", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/processingAgent")
    public R<Void> processingAgent(@Validated(ProcessingAgentGroup.class) @RequestBody HmTodoItemsBo bo) {
        return toAjax(hmTodoItemsService.processingAgent(bo));
    }

    /**
     * 数据更新
     */
    @SaCheckPermission("hm:todoItems:edit")
    @Log(title = "代办事项", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/update")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmTodoItemsBo bo) {
        return toAjax(hmTodoItemsService.updateByBo(bo));
    }

    /**
     * 删除代办事项
     *
     * @param todoIds 主键串
     */
    @SaCheckPermission("hm:todoItems:remove")
    @Log(title = "代办事项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{todoIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] todoIds) {
        return toAjax(hmTodoItemsService.deleteWithValidByIds(List.of(todoIds), true));
    }
}
